﻿using MvvmCross.Core.ViewModels;
using System.Linq;

namespace Sidvall.UI.Navigation
{
	public class HistoryRegionItemCollection : System.Collections.ObjectModel.Collection<HistoryRegionItem>
	{
		#region Public Members

		#region AddItem

		public bool AddItem(HistoryRegionItem item)
		{
			HistoryRegionItem historyRegionItem;

			if (item == null)
				return false;
			historyRegionItem = GetItem(item.RegionName);
			if (historyRegionItem != null)
				return false;
			Add(item);
			return true;
		}

		#endregion
		#region GetItem

		public HistoryRegionItem GetItem(string regionName)
		{
			if (string.IsNullOrWhiteSpace(regionName))
				return null;
			return (from o in this
					where o.RegionName.Equals(regionName, System.StringComparison.OrdinalIgnoreCase)
					select o).FirstOrDefault();
		}

		#endregion
		#region GetRegionNames

		public string[] GetRegionNames()
		{
			return (from o in this
					select o.RegionName).Distinct(System.StringComparer.OrdinalIgnoreCase).ToArray();
		}

		#endregion
		#region GetViewModelRequests

		public MvxViewModelRequest[] GetViewModelRequests(string[] regionNames)
		{
			System.Collections.Generic.List<MvxViewModelRequest> requests;

			requests = new System.Collections.Generic.List<MvxViewModelRequest>();
			if (regionNames != null)
			{
				foreach (var item in this)
				{
					if (regionNames.Contains(item.RegionName, System.StringComparer.OrdinalIgnoreCase))
						requests.Add(item.Request);
				}
			}
			else
			{
				foreach (var item in this)
					requests.Add(item.Request);
			}
			return requests.ToArray();
		}

		#endregion

		#endregion
	}
}
